
/* 
 * Some Comments are included
 */
/* INSTRUCTION: This is a command line application. So please execute this template with the following arguments:

		arg[0] = username
		arg[1] = password
*/

/**
 * @author (Your Name Here)
 *
 */
 
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.extensions.*;
import com.google.gdata.data.*;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * This is a test template
 */

  public class Documents {
    
    public static void main(String[] args) {
      
      try {
        
        // Create a new Documents service
        DocsService myService = new DocsService("My Application");
        myService.setUserCredentials(args[0],args[1]);
        
        // Get a list of all entries
        URL metafeedUrl = new URL("http://docs.google.com/feeds/default/private/full");
        System.out.println("Getting Documents entries...\n");
        DocumentListFeed resultFeed = myService.getFeed(metafeedUrl, DocumentListFeed.class);
        List<DocumentListEntry> entries = resultFeed.getEntries();
        for(int i=0; i<entries.size(); i++) {
          DocumentListEntry entry = entries.get(i);
          
          System.out.println("\t"+i+" -- Document(" + entry.getType()+"/"+ entry.getResourceId() +"\n" + entry.getTitle().getPlainText()+ ")" +"\n");
          
          // print the timestamp the document was last viewed
          DateTime lastViewed = entry.getLastViewed();
          if (lastViewed != null) {
            System.out.println("  last viewed: " + lastViewed.toUiString());
         
          }

          // print who made the last modification
          LastModifiedBy lastModifiedBy = entry.getLastModifiedBy();
          if (lastModifiedBy != null) {
            System.out.println("  updated by: " +
                lastModifiedBy.getName() + " - " + lastModifiedBy.getEmail());
             }
       // print the parent folder the document is in
          if (!entry.getParentLinks().isEmpty()) {
            System.out.println("  Parent folders: ");
            for (Link link : entry.getParentLinks()) {
              System.out.println("    --" + link.getTitle() + " - " + link.getHref());
            }
          }
       // Files such as PDFs take up quota
          if (entry.getQuotaBytesUsed() > 0) {
            System.out.println("Quota used: " + entry.getQuotaBytesUsed() + " bytes");
          }

          // print other useful metadata
          System.out.println("  last updated: " + entry.getUpdated().toUiString());
          System.out.println("  viewed by user? " + entry.isViewed());
          System.out.println("  writersCanInvite? " + entry.isWritersCanInvite().toString());
          System.out.println("  hidden? " + entry.isHidden());
          System.out.println("  starred? " + entry.isStarred());
          System.out.println("  trashed? " + entry.isTrashed());
          System.out.println("  folders " + entry.getParentLinks());
          System.out.println();
          
        }
        // print the total number of Entries
        System.out.println("\nTotal Entries: "+entries.size());
      }
      catch(AuthenticationException e) {
        e.printStackTrace();
      }
      catch(MalformedURLException e) {
        e.printStackTrace();
      }
      catch(ServiceException e) {
        e.printStackTrace();
      }
      catch(IOException e) {
        e.printStackTrace();
      }
    }
  }
